package com.wangjiang.mario.admin.system.domain.service.impl;

import com.wangjiang.mario.admin.common.base.BaseServiceImpl;
import com.wangjiang.mario.admin.common.exception.CreateException;
import com.wangjiang.mario.admin.common.util.CommonUtils;
import com.wangjiang.mario.admin.common.util.DateUtil;
import com.wangjiang.mario.admin.common.util.FileUtils;
import com.wangjiang.mario.admin.common.util.StringUtils;
import com.wangjiang.mario.admin.system.domain.components.ConfigComponent;
import com.wangjiang.mario.admin.system.domain.entity.WxMicroappEntity;
import com.wangjiang.mario.admin.system.domain.entity.WxMicroappQrcodeEntity;
import com.wangjiang.mario.admin.system.domain.mapper.WxMicroappQrcodeMapper;
import com.wangjiang.mario.admin.system.domain.service.FileService;
import com.wangjiang.mario.admin.system.domain.service.WxMicroappQrcodeService;
import com.wangjiang.mario.admin.system.domain.service.WxMicroappService;
import com.wangjiang.mario.admin.system.domain.util.WxAppUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;

@Service
public class WxMicroappQrcodeServiceImpl extends BaseServiceImpl<WxMicroappQrcodeMapper, WxMicroappQrcodeEntity>
        implements WxMicroappQrcodeService {

    private static final Logger logger = LoggerFactory.getLogger(WxMicroappQrcodeServiceImpl.class);

    // 生成小程序二维码方式1
    private static final String CREATE_QRCODE = "createQRCode";
    // 生成小程序二维码方式2
    private static final String GET_QRCODE = "get";

    private static final String GET_UNLIMITED_QRCODE = "getUnlimited";

    @Autowired
    private WxMicroappService microappService;

    @Autowired
    private ConfigComponent configComponent;

    @Autowired
    private FileService fileService;
    /**
     * 生成二维码
     *
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public WxMicroappQrcodeEntity generateQRCode(String appid, WxMicroappQrcodeEntity entity) {
        WxMicroappEntity app = microappService.getByAppid(appid);
        entity.setMicroappCode(app.getCode());
        String accessToken = microappService.getAccessToken(appid, app);// token
        // 定义二维码的名字
        String qrcodeImgName = generateQRCodeName(appid);
        try {
            if (CommonUtils.isNull(entity.getGenerateWay()) || Objects.equals(CREATE_QRCODE, entity.getGenerateWay())) {
                // 普通二维码
                InputStream inputStream = WxAppUtil.createQRCode(accessToken, entity.getPath(), entity.getWidth());
                // 保存图片在本地服务器
                FileUtils.saveToImgByInputStream(inputStream, configComponent.getUploadRootPath(), qrcodeImgName);
            } else if (Objects.equals(GET_QRCODE, entity.getGenerateWay())) {
                InputStream inputStream = WxAppUtil.getQRCode(accessToken, entity.getPath(), entity.getWidth());
                // 保存图片在本地服务器
                FileUtils.saveToImgByInputStream(inputStream, configComponent.getUploadRootPath(), qrcodeImgName);
            } else if (Objects.equals(GET_UNLIMITED_QRCODE, entity.getGenerateWay())) { // 只有三种
                // TODO 菊花码 这里暂时调不通
                InputStream inputStream = WxAppUtil.getUnlimitedQRCode(accessToken, entity.getPath(), entity.getWidth(), "");
                // 保存图片在本地服务器
                FileUtils.saveToImgByInputStream(inputStream, configComponent.getUploadRootPath(), qrcodeImgName);
            }
            // 保存文件上传记录
            fileService.justSaveAsALog(qrcodeImgName);
            entity.setQrcode(qrcodeImgName);
            baseSave(entity);
        } catch (IOException e) {
            logger.error(e.getMessage());
            throw new CreateException("创建二维码图片失败");
        }
        return entity;
    }

    private String generateQRCodeName(String appid) {
        return StringUtils.format("{}_{}.png", appid, DateUtil.getNowDate().getTime());
    }


}
